# -*- coding: utf-8 -*-
"""
Created on Thu Dec  7 01:52:43 2023

@author: asus

这份文件是通过循环获得不同航班到达率下
仿真系统的输出结果：

- 平均着陆等待时间
- 平均队列长度
"""

import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

import simpy

from pylab import mpl
# 指定默认字体：解决plot不能显示中文问题
mpl.rcParams['font.sans-serif'] = ['SimHei'] 
# 解决保存图像是负号'-'显示为方块的问题
mpl.rcParams['axes.unicode_minus'] = False 

from AirportSimulator import *
from Airplane import *

RANDOM_SEED = 42

NUM_RUNWAY = 1
NUM_PARKING_BAY = 42
NUM_GROUND_SERVICE_GROUP = 35

res_dict = {
    'average_arrival' : [],
    'average_wait_time' : [],
    'average_queue_len' : []
    }

random.seed(RANDOM_SEED)

for minutes_times_10 in range(1, 20):
    AVERAGE_ARRIVIAL_TIME = (minutes_times_10 / 10) * 60
    NUM_CUSTOMER = int(float(60 * 60) / AVERAGE_ARRIVIAL_TIME)
    
    env = simpy.Environment()
    simAirPort = AirportSimulator(
        env, 
        num_customer=NUM_CUSTOMER,
        average_arrival_time=AVERAGE_ARRIVIAL_TIME,
        num_runways=NUM_RUNWAY,
        num_parking_bays=NUM_PARKING_BAY,
        num_ground_services_groups=NUM_GROUND_SERVICE_GROUP,
        record_queue_len=True
    )

    env.run()
    
    sim_res_data = simAirPort.result
    queue_len_df = simAirPort.queue_len_df
    
    res_dict['average_arrival'].append(
        AVERAGE_ARRIVIAL_TIME)
    res_dict['average_wait_time'].append(
        sim_res_data['landing_wait_time'].mean())
    res_dict['average_queue_len'].append(
        queue_len_df['runways'].mean())
    
res_df = pd.DataFrame(res_dict)
res_df.to_csv('./data/final_res.csv')

# In[3]:
fig, ax = plt.subplots(dpi=300)
ax.plot(
    (res_df['average_arrival'] / 60),
    (res_df['average_wait_time'] / 60),
    label='平均等待时间'
    )
ax.set_title("平均等待时间与平均到达时间间隔关系")
ax.set_xlabel("平均到达时间间隔（分钟）")
ax.set_ylabel("平均等待时间（分钟）")
ax.axhline(20, label='20 分钟线', c='r', ls='--')
ax.legend()